package com.dlc.felear.xiaoensale.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.EmnuListtemAdapter;
import com.dlc.felear.xiaoensale.adapter.LeftMenuCatAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.dao.CookingMenuDao;
import com.dlc.felear.xiaoensale.dao.OperatorDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.entity.Operator;
import com.dlc.felear.xiaoensale.helper.CookingMenuMangerHelper;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.helper.Session;
import com.dlc.felear.xiaoensale.utils.InputMethodManagerUtil;
import com.itdlc.android.library.base.BaseActivity;
import com.itdlc.android.library.utils.NetStateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 炒菜菜谱显示页面
 */
public class GdCookingMenuActivity extends BaseActivity {
    @BindView(R.id.e_gv)
    GridView gv;
    @BindView(R.id.c_lv)
    ListView lv;
    @BindView(R.id.et_search)
    EditText etSeach;
    private CookingMenuDao cookingMenuDao;
    private EmnuListtemAdapter adapter;
    private CookingMenu cookingMenu;
    private TopbarHelper topbarHelper;
    private LeftMenuCatAdapter dialogCatAdapter;
    private CookingMenuMangerHelper cookingMenuMangerHelper;
    private String deviceType = "M400";
    private OperatorDao operatorDao;


    @Override
    protected int getLayoutRes() {
        return R.layout.activity_menu_cooking;
    }

    @Override
    protected void initialView() {
        deviceType = Session.getSession().getString(Constant.D_GT_TYPE, "M260");
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setNavActivity(CcLauncherActivity.class);
        topbarHelper.setTitle("菜谱列表");
        adapter = new EmnuListtemAdapter(this);
        gv.setAdapter(adapter);
        operatorDao=new OperatorDao(this);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cookingMenu = adapter.getData().get(position);
                if (TextUtils.equals(cookingMenu.getMachineModoel(), deviceType)) {
                    gotoCc(cookingMenu);
                } else {
                    showShortToast("该菜谱设备型号与该机器不符(" + cookingMenu.getMachineModoel() + "_" + deviceType + ")");
                }
            }
        });
    }

    private void gotoCc(CookingMenu cookingMenu){
       try {
           List<Operator> list=operatorDao.queryForAllByMID(cookingMenu.getId());
           if(list==null||list.isEmpty()){
               showShortToast("该菜谱没有炒菜步骤，不能使用，请添加炒菜步骤再使用");
               return;
           }
           Intent intent1 = new Intent(this, CookingActivity.class);
           intent1.putExtra("cookingMenu", JsonHelper.toJson(cookingMenu));
           intent1.putExtra("operators", JsonHelper.toJson(list));
           startActivity(intent1);
       }catch (Exception e){

       }
    }


    @Override
    protected void initData() {
        cookingMenuDao = new CookingMenuDao(this);
        cookingMenuMangerHelper = CookingMenuMangerHelper.getInstance(this, cookingMenuDao);
        initDaoData();
        dialogCatAdapter = new LeftMenuCatAdapter(this);
        lv.setAdapter(dialogCatAdapter);
        dialogCatAdapter.setItems(cookingMenuMangerHelper.getStdCatListNames());
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialogCatAdapter.setSelectitem(position);
                handleCat(dialogCatAdapter.getItem(position));
            }
        });
    }

    private void initDaoData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "data=" + JsonHelper.toJson(checkData(cookingMenuMangerHelper.getCatMenuList(getString(R.string.quanbu), deviceType, Constant.MENU_ALL))));
        adapter.setItems(checkData(cookingMenuMangerHelper.getCatMenuList(getString(R.string.quanbu), deviceType, Constant.MENU_ALL)));
    }

    @OnClick({R.id.btn_ss})
    public void clickView(View view) {
        InputMethodManagerUtil.hideKeyboard(view);
        clickItem("ss");
    }

    private void clickItem(String tag) {
        if (!TextUtils.equals(tag, "ss")) {
            adapter.setItems(checkData(cookingMenuMangerHelper.getCatMenuList(tag, deviceType, Constant.MENU_ALL)));
            return;
        }
        etSeach.setText(etSeach.getText().toString().trim().toUpperCase());
        showLikeName(etSeach.getText().toString().trim().toUpperCase());
    }

    private void showLikeName(String key) {
        List<CookingMenu> tempList = checkData(cookingMenuMangerHelper.getCatMenuList(getString(R.string.quanbu), deviceType, Constant.MENU_ALL));
        if (TextUtils.isEmpty(key)) {
            adapter.setItems(tempList);
            return;
        }
        ArrayList<CookingMenu> ttt = new ArrayList<>();
        for (CookingMenu cookingMenu : tempList) {
            if (cookingMenu.getName().contains(key)) {
                ttt.add(cookingMenu);
            }
        }
        adapter.setItems(ttt);
    }


    private void handleCat(String catName) {
        if (!NetStateUtils.isNetworkConnected(this) && TextUtils.equals(catName, getString(R.string.jinricaipin))) {
            showShortToast("网络未连接，请检查网络");
            return;
        }
        adapter.setItems(checkData(cookingMenuMangerHelper.getCatMenuList(catName, deviceType, Constant.MENU_ALL)));
    }

    private List<CookingMenu> checkData(List<CookingMenu> data) {
        if(data==null||data.isEmpty()){
            return new ArrayList<>();
        }
        List<CookingMenu> tempList=new ArrayList<>();
        try {
            for (CookingMenu cookingMenu : data) {
                Log.e(TAG,"cookingMenu:"+JsonHelper.toJson(cookingMenu));
                if (Integer.parseInt(cookingMenu.getSpecification()) > 1) {
                    tempList.add(cookingMenu);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "checkData Exception:" + e.getMessage());
        }
        return tempList;
    }

}